//
//  ViewController.m
//  LYPhotoBrowser
//
//  Created by LvYuan on 16/12/12.
//  Copyright © 2016年 LvYuan. All rights reserved.
//

#import "ViewController.h"

#import "PhotoCell.h"

#import "UIImageView+WebCache.h"

#import "LYPhotoBrowser.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,LYPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *srcStringArray;
@property (nonatomic, strong) NSMutableArray *photos;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[SDWebImageManager sharedManager].imageCache clearDisk];

    _srcStringArray = @[
                        @"http://ww2.sinaimg.cn/thumbnail/9ecab84ejw1emgd5nd6eaj20c80c8q4a.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/642beb18gw1ep3629gfm0g206o050b2a.gif",
                        @"http://ww4.sinaimg.cn/thumbnail/9e9cb0c9jw1ep7nlyu8waj20c80kptae.jpg",
                        @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg",
                        @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg",
                        @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/677febf5gw1erma104rhyj20k03dz16y.jpg",
                        @"http://ww4.sinaimg.cn/thumbnail/677febf5gw1erma1g5xd0j20k0esa7wj.jpg"
                        ];
    
    _photos = [NSMutableArray array];
    
    [_srcStringArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LYPhoto * photo = [[LYPhoto alloc]init];
        photo.thumbPicUrl = obj;
        photo.largePicUrl = [obj stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        [_photos addObject:photo];
    }];
    
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 9;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"PhotoCell";
    
    PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"PhotoCell" owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.pho = self.photos[indexPath.row];
    
    __weak typeof(self) wself = self;
    
    cell.tapCall = ^(UIImageView * sender){
        //启动图片浏览器
        LYPhotoBrowser *browser = [[LYPhotoBrowser alloc] init];
        browser.sourceView = sender; // 原图的父控件
        browser.imageCount = wself.photos.count; // 图片总数
        browser.currentImageIndex = indexPath.row;
        browser.delegate = wself;
        [browser show];
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 166.f;
}

#pragma mark - photobrowser代理方法

- (LYPhoto*)photoBrowser:(LYPhotoBrowser *)browser photoAtIndex:(NSInteger)index{
    if (index>self.photos.count-1) {
        return nil;
    }
    LYPhoto * photo = [self.photos objectAtIndex:index];
    return photo;
}

@end
